package com.ultrawise.survey.core.base;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ultrawise.survey.core.Page;

/**
 * server 的基类
 */
@Service("baseService")
public class BaseService {
	@Autowired
	private BaseDao baseDao;

	public Object queryByID(String id, Saveable objForSave, Class obj) throws ServiceException {
		try {
			return (Object) baseDao.findByID(id, objForSave, obj);
		} catch (Exception e) {
			throw new ServiceException("查询失败", e);
		}
	}

	public Object queryByID(Integer id, Saveable objForSave, Class obj) throws ServiceException {
		try {
			return (Object) baseDao.findByID(id, objForSave, obj);
		} catch (Exception e) {
			e.printStackTrace();
			// throw new ServiceException("查询失败", e);
			return null;
		}
	}
		
	public Object queryByColunm(String colunm, String value, Saveable objForSave, Class obj) throws ServiceException {
		try {
			return (Object) baseDao.findByColunmObj(colunm, value, objForSave, obj);
		} catch (Exception e) {
			e.printStackTrace();
			// throw new ServiceException("查询失败", e);
			return null;
		}
	}

	public Object queryByColunm2(String colunm, String value, String colunm2, String value2, Saveable objForSave,
			Class obj) throws ServiceException {
		try {
			return (Object) baseDao.findByColunmObj2(colunm, value, colunm2, value2, objForSave, obj);
		} catch (Exception e) {
			throw new ServiceException("查询失败", e);
		}
	}

	public List query(String colunm, String value, Saveable objForSave, Class obj) throws ServiceException {
		try {
			return baseDao.findByColunm(colunm, value, objForSave, obj);
		} catch (Exception e) {
			throw new ServiceException("查询失败", e);
		}
	}

	public Object removeObj(String id, Saveable objForSave) throws ServiceException {
		try {
			return (Object) baseDao.delete(id, objForSave);
		} catch (Exception e) {
			throw new ServiceException("删除失败", e);
		}
	}

	/***
	 * 
	 * 
	 * @param id
	 * @param objForSave
	 * @return
	 * @throws ServiceException
	 */
	public Object removeObj(Integer id, Saveable objForSave) throws ServiceException {
		try {
			return (Object) baseDao.delete(id, objForSave);
		} catch (Exception e) {
			throw new ServiceException("删除失败", e);
		}
	}

	public Object queryAll(Saveable objForSave, Class obj) throws ServiceException {
		try {
			return (Object) baseDao.findAll(objForSave, obj);
		} catch (Exception e) {
			throw new ServiceException("查询失败", e);
		}
	}

	public Object saveObj(Saveable objForSave) throws ServiceException {
		try {
			return (Object) baseDao.insert(objForSave);
		} catch (Exception e) {
			throw new ServiceException("保存失败", e);
		}
	}

	public Object updateObj(Saveable objForSave) throws ServiceException {
		try {
			return (Object) baseDao.update(objForSave);
		} catch (Exception e) {
			throw new ServiceException("修改失败", e);
		}
	}

	// 根据条件查询数据
	public Page getAllData(Map codeMap, Map<String, String> map, String acStatus, Saveable objForSave, final Class obj,
			final int pageNo, final int pageSize) throws ServiceException, DaoException {
		return (Page) baseDao.searchAllData(codeMap, map, acStatus, objForSave, obj, pageNo, pageSize);
	}

	// 获取数据表中的唯一标示的最大值。如果没有返回null
	public Object getMaxID(Saveable objForSave) {
		return baseDao.getMaxID(objForSave);
	}

	// 获取数据表中的唯一标示的最大值。如果没有返回null
	public Object getMaxByColname(Saveable objForSave, String colname) {
		return baseDao.getMaxByColname(objForSave, colname);
	}

}
